/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package sap.gui.chart;

import com.sap.mw.jco.JCO;
import com.sap.mw.jco.JCO.Function;
import java.io.File;
import java.util.Calendar;
import java.util.Date;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.data.category.IntervalCategoryDataset;
import org.jfree.data.gantt.Task;
import org.jfree.data.gantt.TaskSeries;
import org.jfree.data.gantt.TaskSeriesCollection;
import org.jfree.data.time.SimpleTimePeriod;

/**
 *
 * @author Johannes
 */
public class GanttDiagramm {
    public static void paintGantt(String projectdefinition) {
        GanttDiagramm demo = new GanttDiagramm(projectdefinition);
        //demo.pack();
        //RefineryUtilities.centerFrameOnScreen(demo);
        //demo.setVisible(true);
    }

    public GanttDiagramm(String projectdefinition) {
        //super(title);

        IntervalCategoryDataset dataset = createDataset(projectdefinition);
        JFreeChart chart = createChart(dataset);

        // add the chart to a panel...
        //final ChartPanel chartPanel = new ChartPanel(chart);
        //chartPanel.setPreferredSize(new java.awt.Dimension(500, 270));
        //setContentPane(chartPanel);

        try {
        	File file = new File(getClass().getResource("./chart.jpg").toString().substring(6));
            System.out.println(getClass().getResource("./chart.jpg").toString().substring(6));
        	ChartUtilities.saveChartAsJPEG(file, chart, 800, 300);
        } catch (Exception e) {
            System.out.println("Problem occurred creating chart.");
        }
    }


    public static IntervalCategoryDataset createDataset(String projectdefinition) {
    	// Verbindung holen und Parameter setzen
        sap.Connection connection = sap.Connection.getInstance();
        Function fun = connection.getFunction("BAPI_PROJECT_GETINFO");
        fun.getImportParameterList().setValue(projectdefinition, "PROJECT_DEFINITION");
        connection.execute(fun);
        JCO.Table table = fun.getTableParameterList().getTable("E_WBS_ELEMENT_TABLE");

        // Taskseries anlegen
        TaskSeries s1 = new TaskSeries("Scheduled");

        // Daten aus Table auslesen
        for (int i = 0; i < table.getNumRows(); i++) {
			table.setRow(i);
			String str = table.getString("WBS_ELEMENT") +"  " +table.getString("DESCRIPTION");

			Date anfdate = table.getDate("WBS_BASIC_START_DATE");
			int anftag = anfdate.getDate();
			int anfmonat = anfdate.getMonth();
			int anfjahr = anfdate.getYear() + 1900;
			Date enddate = table.getDate("WBS_BASIC_FINISH_DATE");
			int endtag = enddate.getDate();
			int endmonat = enddate.getMonth();
			int endjahr = enddate.getYear() + 1900;

			s1.add(new Task(str,
		               new SimpleTimePeriod(date(anftag,anfmonat,anfjahr),
		                                    date(endtag,endmonat,endjahr))));
		}

        TaskSeriesCollection collection = new TaskSeriesCollection();
        collection.add(s1);

        return collection;
    }


    private static Date date(final int day, final int month, final int year) {

        final Calendar calendar = Calendar.getInstance();
        calendar.set(year, month, day);
        final Date result = calendar.getTime();
        return result;
    }


    private JFreeChart createChart(IntervalCategoryDataset dataset) {
        JFreeChart chart = ChartFactory.createGanttChart(
            "Projektstrukturplan",  // chart title
            "Task",              // domain axis label
            "Date",              // range axis label
            dataset,             // data
            true,                // include legend
            true,                // tooltips
            false                // urls
        );
        return chart;
    }



}
